package com.masterclient.app.businessmodule.minemodule.redpack.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.modules.coupon.beans.RedPackedListBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by TMVPHelper on 2017/08/07
 */
public class RedPackedPresenter extends RedPackedContract.Presenter {

    public List<RedPackedListBean.DataBean.ShItemsBean> fromList;
    @Override
    public void getRedPackedFrom(String page, String page_size) {
        if (fromList == null) {
            mView.baseShowPageLoading(false);
        }
        Observable<RedPackedListBean> observable = RetrofitUtil.getInstance().getRedPackedFrom(page, page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<RedPackedListBean>() {
            @Override
            public void accept(@NonNull RedPackedListBean bean) throws Exception {

                mView.baseHiddenPageLoading();
                if ("0".equals(bean.getCode())) {
                    mView.redPackedFrom(bean.getData());
                } else {
                    ToastUtil.showToast(bean.getMsg() != null && bean.getMsg().length() > 0 ? bean.getMsg() : WEB_FAILED_STR);
                    mView.loadFailed();
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                ToastUtil.showToast(WEB_FAILED_STR);
                mView.loadFailed();
            }
        });
        mRxManager.add(disposable);
    }

    public List<RedPackedListBean.DataBean.ShItemsBean> payList;
    @Override
    public void getRedPackedPayFor(String page, String page_size) {
        if (payList == null) {
            mView.baseShowPageLoading(false);
        }
        Observable<RedPackedListBean> observable = RetrofitUtil.getInstance().getRedPackedPayFor(page, page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<RedPackedListBean>() {
            @Override
            public void accept(@NonNull RedPackedListBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                if ("0".equals(bean.getCode())) {
                    mView.redPackedpayFor(bean.getData());
                } else {
                    ToastUtil.showToast(bean.getMsg() != null && bean.getMsg().length() > 0 ? bean.getMsg() : WEB_FAILED_STR);
                    mView.loadFailed();
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                ToastUtil.showToast(WEB_FAILED_STR);
                mView.loadFailed();
            }
        });
        mRxManager.add(disposable);
    }
}